#include"reservation.h"

Reservation::Reservation() {
	this->m_Size = 0;
	this->initialReservation();

	/*for (map<int, map<string, string>>::iterator it = this->m_Reservations.begin();it != this->m_Reservations.end();it++) {
		for (map<string, string>::iterator it2 = it->second.begin();it2 != it->second.end();it2++) {
			cout << "key:" << it2->first << " value:" << it2->second << endl;
		}
	}*/
}

void Reservation::initialReservation() {
	ifstream ifs;
	ifs.open(SUB_RER_INFO, ios::in);
	if (!ifs.is_open()) {
		cout << "File is empty" << endl;
		system("pause");
		system("cls");
		return;
	}

	string date;
	string interval;
	string studId;
	string studName;
	string room;
	string status;

	while (ifs >> date && ifs >> interval && ifs >> studId && ifs >> studName && ifs >> room && ifs >> status) {
		map<string, string> smap;
		string key;
		string value;
		int pos;

		pos = date.find(":");
		if (pos != -1) {
			key = date.substr(0, pos);
			value = date.substr(pos + 1, date.size() - 1 - pos);

			smap.insert(make_pair(key, value));
		}

		pos = interval.find(":");
		if (pos != -1) {
			key = interval.substr(0, pos);
			value = interval.substr(pos + 1, interval.size() - 1 - pos);

			smap.insert(make_pair(key, value));
		}

		pos = studId.find(":");
		if (pos != -1) {
			key = studId.substr(0, pos);
			value = studId.substr(pos + 1, studId.size() - 1 - pos);

			smap.insert(make_pair(key, value));
		}

		pos = studName.find(":");
		if (pos != -1) {
			key = studName.substr(0, pos);
			value = studName.substr(pos + 1, studName.size() - 1 - pos);

			smap.insert(make_pair(key, value));
		}

		pos = room.find(":");
		if (pos != -1) {
			key = room.substr(0, pos);
			value = room.substr(pos + 1, room.size() - 1 - pos);

			smap.insert(make_pair(key, value));
		}

		pos = status.find(":");
		if (pos != -1) {
			key = status.substr(0, pos);
			value = status.substr(pos + 1, status.size() - 1 - pos);

			smap.insert(make_pair(key, value));
		}
		
		this->m_Reservations.insert(make_pair(this->m_Size, smap));
		this->m_Size++;
	}
	ifs.close();
}

//update subscription
void Reservation::updateSub() {
	if (this->m_Size == 0) {
		return;
	}
	
	ofstream ofs;
	ofs.open(SUB_RER_INFO, ios::out | ios::trunc);
	for (int i = 0;i < this->m_Size;i++) {
		ofs << "date:" << this->m_Reservations[i]["date"] << " ";
		ofs << "interval:" << this->m_Reservations[i]["interval"] << " ";
		ofs << "studId:" << this->m_Reservations[i]["studId"] << " ";
		ofs << "studName:" << this->m_Reservations[i]["studName"] << " ";
		ofs << "room:" << this->m_Reservations[i]["room"] << " ";
		ofs << "status:" << this->m_Reservations[i]["status"] << endl;
	}
	ofs.close();
}

